package com.basics.logging.controller;
import com.basics.common.utils.SecurityUtils;
import com.basics.logging.aop.log.EnableBasicsLog;
import com.basics.logging.service.LogService;
import com.basics.logging.service.dto.LogQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**功能描述：日志模块控制层
 * @Author Geordie
 * @ClassName LogController
 * @Date 2021/7/8
 * @Annotation
 */
@RestController
@RequestMapping("/api/logs")
@Api(tags = "监控：日志管理")
public class LogController {
    @Resource
    private LogService logService;

    @GetMapping
    @ApiOperation("操作日志查询")
    public ResponseEntity<Object> getLogs(LogQueryCriteria criteria, @PageableDefault(page=1,size=50,sort="create_time asc")Pageable pageable){
        criteria.setLogType("INFO");
        return new ResponseEntity<>(logService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    @DeleteMapping(value = "/del/info")
    @EnableBasicsLog("删除所有INFO日志")
    @ApiOperation("删除所有INFO日志")
    public ResponseEntity<Object> delAllByInfo(){
        logService.delAllByInfo();
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @GetMapping(value = "/error")
    @ApiOperation("错误日志查询")
    public ResponseEntity<Object> getErrorLogs(LogQueryCriteria criteria, @PageableDefault(page=1,size=50,sort="create_time asc")Pageable pageable){
        criteria.setLogType("ERROR");
        return new ResponseEntity<>(logService.queryAll(criteria,pageable), HttpStatus.OK);
    }
    @GetMapping(value = "/error/{id}")
    @ApiOperation("日志异常详情查询")
    public ResponseEntity<Object> getErrorLogs(@PathVariable Long id){
        return new ResponseEntity<>(logService.findByErrDetail(id), HttpStatus.OK);
    }
    @DeleteMapping(value = "/del/error")
    @EnableBasicsLog("删除所有ERROR日志")
    @ApiOperation("删除所有ERROR日志")
    public ResponseEntity<Object> delAllByError(){
        logService.delAllByError();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/user")
    @ApiOperation("用户日志查询")
    public ResponseEntity<Object> getUserLogs(LogQueryCriteria criteria, @PageableDefault(page=1,size=50,sort="create_time asc") Pageable pageable){
        criteria.setLogType("INFO");
        criteria.setUserName(SecurityUtils.getUsername());
        return new ResponseEntity<>(logService.queryAllByUser(criteria,pageable), HttpStatus.OK);
    }

}
